package com.home.duy.code.DAO;

import java.util.List;


import org.hibernate.Session;
import org.hibernate.Transaction;

import com.home.Entity.CTQuyenSuDung;
import com.home.Entity.QuyenSuDung;
import com.home.System.HibernateUtil;
import com.home.duy.code.Abstract.AbstractDAO;

public class CTQuyenSuDungDAO extends AbstractDAO {
	private Session session;
	private Transaction trans;

	public CTQuyenSuDungDAO() {
		super();
	}

	@Override
	public boolean saveOrUpdate(Object obj) {
		return super.saveOrUpdate(obj);
	}

	@Override
	public boolean delete(Object obj) {
		return super.delete(obj);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Object findByPk(Class clas, long pk) {
		return super.findByPk(clas, pk);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List findAll(Class clas) {
		return super.findAll(clas);
	}

	@SuppressWarnings("unchecked")
	public CTQuyenSuDung findByTKandQSD(long tk, long qsd) {
		startOperation();
		CTQuyenSuDung ctQsd = null;
		try {
			List<CTQuyenSuDung> list = null;
			list = session
			.createSQLQuery(
					"SELECT * FROM CTQUYENSUDUNG WHERE CTQ_TAIKHOAN = "
							+ tk + " AND CTQ_QUYEN = " + qsd)
			.addEntity(CTQuyenSuDung.class).list();
			if(list.size() > 0)
			{
				ctQsd = (CTQuyenSuDung)list.get(0);
			}
			trans.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ctQsd;
	}
	
	private void startOperation()
	{
		session = HibernateUtil.getSessionFactory().openSession();
		trans = session.beginTransaction();
	}
}
